<?php
class Dig_Applet_Controller
{
    protected $_options = null;
    protected $_applet;
    protected $view;
    /**
     * this method runs the applet and returns the xhmtml response
     *
     * @param string $applet
     * @param array $options
     * @return XHTML
     */
    public static function dispatch ($applet, $options = array())
    {
        $controller = self::loadController($applet);
        $controller->setOptions($options, self::getProperties($controller));
        $view = $controller->getView();
        $view->setScriptPath(self::getPathToApplet($applet));
        // run the controllers onLoad() and onPost() (if this is a post back) methods
        $controller->onLoad();
        if (Zend_Controller_Front::getInstance()->getRequest()->isPost()) {
            $controller->onPost();
        }
        // render the page
        return $view->render('view.phtml');
    }
    /**
     * this method loads the applet controller class
     *
     * @param string $applet
     * @return Dig_Applet_Controller
     */
    public static function loadController ($applet)
    {
        $pathToApplet = self::getPathToApplet($applet);
        require_once $pathToApplet . '/Controller.php';
        $controllerName = zend_filter::filterStatic($applet, 'Word_UnderscoreToCamelCase') . '_Controller';
        return new $controllerName();
    }
    /**
     * sets the applet options. it only sets item that are the property list
     *
     * @param array $options
     * @param array $properties
     */
    public function setOptions ($options = array(), $properties = array())
    {
        if (count($properties) > 0) {
            foreach ($properties as $property) {
                $this->$property = isset($options[$property]) ? $options[$property] : null;
            }
        }
        $this->_options = $options;
    }
    /**
     * creates an array of the public properties of the controller class
     *
     * @param Dig_Applet_Controller $controller
     * @return array
     */
    public static function getProperties ($controller)
    {
        $class = new Zend_Reflection_Class($controller);
        $properties = $class->getProperties();
        $publicProperties = array();
        foreach ($properties as $property) {
            if ($property->isPublic()) {
                $publicProperties[] = $property->getName();
            }
        }
        return $publicProperties;
    }
    /**
     * returns the full path to the applet
     *
     * @param string $applet
     * @return string
     */
    public static function getPathToApplet ($applet)
    {
        // validate and load the applet controller
        $pathToApplet = APPLICATION_PATH . '/applets/' . $applet;
        if (! is_dir($pathToApplet)) {
            throw new Dig_Exception('Invalid applet');
        }
        return $pathToApplet;
    }
    /**
     * Get current view object
     *
     * If no view object currently set, retrieves it from the ViewRenderer.
     * 
     * @todo Set inflector from view renderer at same time
     * @return Zend_View_Interface
     */
    public function getView ()
    {
        if (null === $this->view) {
            require_once 'Zend/Controller/Action/HelperBroker.php';
            $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
            if (null === $viewRenderer->view) {
                $viewRenderer->initView();
            }
            $this->setView($viewRenderer->view);
        }
        return $this->view;
    }
    /**
     * Set view object
     * 
     * @param  Zend_View_Interface $view
     * @return Zend_Layout
     */
    public function setView (Zend_View_Interface $view)
    {
        $this->view = $view;
        return $this;
    }
    public function onLoad ()
    {}
    public function onPost ()
    {}
}
?>